/**
 * File: MaqMatchController.java
 * Created by: mhaimel
 * Created on: 7 Jul 2009
 * CVS:  $Id: MaqMatchController.java,v 1.1 2009/08/05 15:16:12 mhaimel Exp $
 */
package uk.ac.ebi.curtain.controller.maq.impl;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import uk.ac.ebi.curtain.controller.ParallelController;
import uk.ac.ebi.curtain.controller.SimpleReport;
import uk.ac.ebi.curtain.controller.maq.MaqBfqOutputBean;
import uk.ac.ebi.curtain.controller.maq.MaqContigOutputBean;
import uk.ac.ebi.curtain.controller.maq.MaqMatchInputBean;
import uk.ac.ebi.curtain.management.IControllerReport;
import uk.ac.ebi.curtain.management.InterceptorInfo;
import uk.ac.ebi.curtain.processor.maq.impl.MaqMatchProcessor;
import uk.ac.ebi.curtain.utils.CurtainUncheckedException;
import uk.ac.ebi.curtain.utils.data.DataEntry;

/**
 * @author mhaimel
 *
 */
public class MaqMatchController extends ParallelController<Serializable, DataEntry<File>, MaqMatchProcessor> {
	private Log log = LogFactory.getLog(this.getClass());
	private Set<File> contigBfaFiles = new HashSet<File>();
	private List<MaqBfqOutputBean> readBfqFilePairs = new ArrayList<MaqBfqOutputBean>();
	private int iCnt = 0;

	@Override
	public synchronized void handle(Serializable input, InterceptorInfo info) {
		if(info.getOriginName().equalsIgnoreCase("maq_bfq")){
			handleRead((MaqBfqOutputBean) input);
		} else if (info.getOriginName().equalsIgnoreCase("maq_contig")){
			handleContig((MaqContigOutputBean) input);
		} else {
			throw new CurtainUncheckedException("Input from controller " + info.getOriginName() + " not supported!!!");
		}
	}
	
	private void handleContig(MaqContigOutputBean input){
		for(File bfa : input.getContigBfaList()){
			if(!contigBfaFiles.contains(bfa)){
				submitContig(bfa);
				contigBfaFiles.add(bfa);
			}
		}
	}
	
	private void submitContig(File bfa) {
		for(MaqBfqOutputBean bfqPair : readBfqFilePairs){
			submit(bfa,bfqPair);
		}
	}	
	
	private void submit(File bfa, MaqBfqOutputBean input){
		++iCnt;
		MaqMatchInputBean bean = new MaqMatchInputBean();
		bean.setDir(input.getBaseDir());
		bean.setBfa(bfa);
		bean.setBfqPair(input.getBfqPairList());
		bean.setReadFiles(input.getReadFiles());
		MaqMatchProcessor process = getContext().getInjector().getInstance(MaqMatchProcessor.class);
		process.setInput(bean);
		submit(process);
	}

	private void handleRead(MaqBfqOutputBean input){
		for(File bfa : contigBfaFiles){
			submit(bfa,input);
		}
		readBfqFilePairs.add(input);
	}	
	


	@Override
	protected MaqMatchProcessor buildJob(Serializable input, InterceptorInfo info) {
		throw new NotImplementedException("Should not be called - handle overwritten!!!");
	}

	@Override
	protected IControllerReport getReturnCall() {
		return new SimpleReport("Finished - submitted processes: " + iCnt);
	}
}
